<?php
    require_once("../config/config.php");
    require_once(DATA_PATH."connect.php");

    function del_allcache($path){
            if(is_dir($path)){
                    $dp=dir($path);
                    while($file=$dp->read())
                            if($file!='.'&&$file!='..')
                                    del_allcache($path.'/'.$file);
                    $dp->close();
            }else{
                    delfile($path);
            }
    }

    //キャッシュファイルを取得
    function delfile($filename){
        //echo $filename."\n<br />";
        if( file_exists( $filename )){
            // 24時間以上経過してる？
            clearstatcache();
            $fTime  = @filemtime( $filename );
            //$ckTiel  = strtotime( "-15minutes" );
            $ckTiel  = strtotime( "-".CACHE_TIME );
            if( $fTime && $fTime <= $ckTiel ){
                unlink($filename);
            }
        }
    }
	function deldata(){
		$dbconn = new connect();
		$test = $dbconn->query('Describe `history` `update_time` ');
		$test = $dbconn->Result();
		if(empty($test)){
			$dbconn->query("ALTER TABLE  `history` ADD  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;");
		}
		$test = $dbconn->query('Describe `price_cache` `update_time` ');
		$test = $dbconn->Result();
		if(empty($test)){
			$dbconn->query("ALTER TABLE  `price_cache` ADD  `update_time` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;");
		}
		$dbconn->query("DELETE FROM `history` WHERE UNIX_TIMESTAMP(`update_time` ) < UNIX_TIMESTAMP(NOW()) - 3600 * 24");
		$dbconn->query("DELETE FROM `price_cache` WHERE UNIX_TIMESTAMP(`update_time` ) < UNIX_TIMESTAMP(NOW()) - 3600 * 24");
	}
	$SERVERURL = (isset($_SERVER['HTTP_HOST']))?$_SERVER['HTTP_HOST']:"";
	try{
		if($SERVERURL==""){
			del_allcache(CACHE_PATH);
			deldata();
			//del_allcache("D:\WorkSpace\PHP\search\cache");
			//print_r($_SERVER);
		}else{
			//del_allcache(CACHE_PATH);
			//echo $_SERVER['HTTP_HOST'];
		}
	}catch(Exception $e) { }